import { JKManager } from "../JKManager";
import { LocalizedLabel } from "../Language/LocalizedLabel";

const { ccclass, property } = cc._decorator;

/**
 * 公共Loading组件
 */
const ActiveActionTime = 0.2, // 显隐动画时间，秒
    LoadingActionTime = 0.05 // loading动画时间，秒

@ccclass
export class LoadingCpnt extends cc.Component {
    private _ndCtn: cc.Node;
    private _ndLoading: cc.Node;
    private _labMsg: LocalizedLabel;
    private _timeLoading: number;
    private _tempTimeLoading: number;
    private _hiding: boolean;
    private _showing: boolean;

    onLoad(): void {
        cc.log(`${cc.js.getClassName(this)} onLoad`)

        this._ndCtn = cc.find(`ndCtn`, this.node)
        this._ndLoading = cc.find(`ndLoading`, this._ndCtn)
        this._labMsg = cc.find(`labMsg`, this._ndCtn).getComponent(LocalizedLabel)
    }

    start(): void {
        cc.log(`${cc.js.getClassName(this)} start`)

        this._labMsg.textKey = `Loading`
        this._ndLoading.angle = 0
        this._timeLoading = LoadingActionTime
        this._tempTimeLoading = 0
    }

    update(dt: number): void {
        this._tempTimeLoading += dt
        if (this._tempTimeLoading >= this._timeLoading) {
            if (this._ndLoading.angle <= -360) {
                this._ndLoading.angle += 360
            }
            this._ndLoading.angle -= 360 / 12

            this._tempTimeLoading = 0
        }
    }

    close(): void {
        cc.log(`${cc.js.getClassName(this)} close`)

        JKManager.UiManager.closeTipView()
    }

    onDestroy(): void {
        cc.log(`${cc.js.getClassName(this)} onDestroy`)

    }

    // ====================================================================================
    // ===================================== 内部接口 ======================================
    // ====================================================================================

    // ====================================================================================

    // ====================================================================================
    // ===================================== 外部接口 ======================================
    // ====================================================================================

    init(data: { msg: string; } = null): void {
        cc.log(`${cc.js.getClassName(this)} init data = `, data)

        // data = {
        //     msg: ``,
        // }
        let msg = data.msg || ``

        this._ndLoading.angle = 0
        this._timeLoading = 0.1
        this._tempTimeLoading = 0
        this._labMsg.textKey = msg
    }

    /**
     * 显示
     * @param call 回调
     * @param actionTime 动画时间
     */
    show(actionTime: number = ActiveActionTime, call: () => void = null): void {
        cc.log(`${cc.js.getClassName(this)} show`)
        // if (!this._showing) {
        //     this._showing = true;
            this.node.active = true;
        //     cc.Tween.stopAllByTarget(this._ndCtn);
        //     cc.tween(this._ndCtn)
        //         .to(actionTime, {
        //             scale: 1,
        //         })
        //         .call(() => {
        //             this._showing = false;
        //             call && call()
        //         })
        //         .start()
        // }
    }

    /**
     * 隐藏
     * @param call 回调
     * @param actionTime 动画时间
     */
    hide(actionTime: number = ActiveActionTime, call: () => void = null): void {
        cc.log(`${cc.js.getClassName(this)} hide`)

        // if (!this._hiding) {
        //     this._hiding = true;
        //     cc.Tween.stopAllByTarget(this._ndCtn);
        //     cc.tween(this._ndCtn)
        //         .to(actionTime, {
        //             scale: 0,
        //         })
        //         .call(() => {
        //             this._hiding = false
                    this.node.active = false
        //             call && call()
        //         })
        //         .start()
        // }
    }
}